import re
import pandas
import os.path


csv_folder_path = '/Users/zhubin/Desktop/宁波58招聘/'


def process_data(path: str):
    file_path = os.path.join('%s%s' % (csv_folder_path, path))
    csvFile = pandas.read_csv(file_path)  # type: DataFrame
    csvFile['薪资下限'] = None
    csvFile['薪资上限'] = None
    lines = csvFile.shape[0]
    for i in range(lines):
        salaryString = csvFile['薪资范围'][i:i + 1][i]  # type: str
        pattern = '[\d]+-[\d]+元/月'
        mathRes = re.match(pattern, salaryString)
        if re.match(pattern, salaryString):
            print(salaryString)
            compilePattern = re.compile('[\d]+')
            resultList = compilePattern.findall(salaryString)  # type: list

            if len(resultList) == 2:
                lowerLimit = resultList[0]  # type: str
                upperLimit = resultList[1]  # type: str
                csvFile['薪资下限'][i:i + 1][i] = lowerLimit
                csvFile['薪资上限'][i:i + 1][i] = upperLimit

    NONE_VIN = (csvFile["薪资下限"].isnull()) | (csvFile["薪资下限"].apply(lambda x: str(x).isspace()))
    csvFile = csvFile[~NONE_VIN]

    out_path = '%s处理后数据/%s' % (csv_folder_path, file)
    csvFile.to_csv(out_path, index=None, encoding="utf_8_sig")


path_dir = os.listdir(csv_folder_path)
for file in path_dir:
    if file.endswith('.csv'):
        print(file)
        process_data(file)


exit()
